{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "aba05718",
   "metadata": {},
   "source": [
    "### 运行之前，请先看[前置工作](./00-construct_MT-Datasets.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8992b3f4",
   "metadata": {},
   "source": [
    "## 这里是为生成landmarks文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bbc948ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "EleGANt_dir = '../EleGANt'\n",
    "import sys\n",
    "sys.path.append(EleGANt_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a8c86549",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import argparse\n",
    "import torch\n",
    "import tqdm\n",
    "import glob\n",
    "from PIL import Image\n",
    "from ele_training.config import get_config\n",
    "from ele_training.preprocess import PreProcess"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "805c9aa2",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_root = './res/user_MT-Dataset/'\n",
    "makeup_paths = glob.glob('%s/images/makeup/*.png' %(data_root))\n",
    "non_makeup_paths = glob.glob('%s/images/non-makeup/*.png' %(data_root))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bbed581b",
   "metadata": {},
   "outputs": [],
   "source": [
    "config = get_config()\n",
    "preprocessor = PreProcess(config, device='cuda:0')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c4b2584a",
   "metadata": {},
   "outputs": [],
   "source": [
    "if not os.path.exists(os.path.join(data_root, 'lms')):\n",
    "    os.makedirs(os.path.join(data_root, 'lms', 'makeup'))\n",
    "    os.makedirs(os.path.join(data_root, 'lms', 'non-makeup'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ccbd2c74",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Processing makeup images...\n",
      "Done.\n"
     ]
    }
   ],
   "source": [
    "print(\"Processing makeup images...\")\n",
    "for makeup_path in makeup_paths:\n",
    "    base_name = os.path.basename(makeup_path).split('.')[0]\n",
    "    raw_image = Image.open(makeup_path).convert('RGB')\n",
    "    lms = preprocessor.lms_process(raw_image)\n",
    "    if lms is not None:\n",
    "        preprocessor.save_lms(lms, os.path.join(data_root, 'lms', 'makeup', f'{base_name}.npy'))\n",
    "print(\"Done.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d0fccc26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Processing non-makeup images...\n",
      "Done.\n"
     ]
    }
   ],
   "source": [
    "print(\"Processing non-makeup images...\")\n",
    "for non_makeup_path in non_makeup_paths:\n",
    "    base_name = os.path.basename(non_makeup_path).split('.')[0]\n",
    "    raw_image = Image.open(non_makeup_path).convert('RGB')\n",
    "    lms = preprocessor.lms_process(raw_image)\n",
    "    if lms is not None:\n",
    "        preprocessor.save_lms(lms, os.path.join(data_root, 'lms', 'non-makeup', f'{base_name}.npy'))\n",
    "print(\"Done.\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch_1.13.1",
   "language": "python",
   "name": "torch_1.13.1"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
